home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / mui / mui-tools / multiuser / src / support / mprotect.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  6KB  |  237 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * Protect Clone                                                            *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13. #include <dos/dosasl.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <libraries/multiuser.h>
  17. #include <proto/multiuser.h>
  18.  
  19. #include "MProtect_rev.h"
  20.  
  21. #include "Locale.h"
  22.  
  23. char __VersTag__[] = VERSTAG;
  24.  
  25.  
  26. #define FLAGS_OWNER    1
  27. #define FLAGS_GROUP    2
  28. #define FLAGS_OTHER    3
  29.  
  30.  
  31. static BOOL __regargs ProcessFlags(char *str, ULONG *flags, ULONG type,
  32.                                               struct DosLibrary *DOSBase,
  33.                                    struct LocaleInfo *li,
  34.                                               struct ExecBase *SysBase);
  35.  
  36.  
  37. int __saveds Start(char *arg)
  38. {
  39.     struct ExecBase *SysBase;
  40.     struct DosLibrary *DOSBase;
  41.     struct muBase *muBase = NULL;
  42.     struct RDArgs *args;
  43.     LONG argarray[] = {
  44.         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
  45.     };
  46.     ULONG mask, flags = NULL;
  47.     struct AnchorPath *anchor;
  48.     BPTR dir;
  49.     LONG error = NULL;
  50.     int rc = RETURN_OK;
  51.     struct LocaleInfo li;
  52.  
  53.     SysBase = *(struct ExecBase **)4;
  54.     
  55.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  56.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  57.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  58.         goto Exit;
  59.     }
  60.  
  61.     OpenLoc(&li);
  62.  
  63.     args = ReadArgs("FILE/A,FLAGS,GROUP/K,OTHER/K,ADD/S,SUB/S,ALL/S,QUIET/S",
  64.                          argarray, NULL);
  65.     if (!args)
  66.         error = IoErr();
  67.     else if (argarray[4] && argarray[5]) {
  68.         PutStr(GetLocS(&li,MSG_BOTH_ADDSUB));
  69.         rc = RETURN_ERROR;
  70.     } else if ((argarray[1] && !ProcessFlags((char *)argarray[1], &flags,
  71.                                                           FLAGS_OWNER, DOSBase, &li, SysBase)) ||
  72.                   (argarray[2] && !ProcessFlags((char *)argarray[2], &flags,
  73.                                                           FLAGS_GROUP, DOSBase, &li, SysBase)) ||
  74.                   (argarray[3] && !ProcessFlags((char *)argarray[3], &flags,
  75.                                                           FLAGS_OTHER, DOSBase, &li, SysBase)))
  76.         rc = RETURN_ERROR;
  77.     else if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+1024,
  78.                                                                     MEMF_CLEAR)) {
  79.         anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  80.         anchor->ap_Flags = APF_DOWILD;
  81.         anchor->ap_Strlen = 1024;
  82.         if (!(error = MatchFirst((char *)argarray[0], anchor))) {
  83.             do
  84.                 if (anchor->ap_Flags & APF_DIDDIR)
  85.                     anchor->ap_Flags &= ~APF_DIDDIR;
  86.                 else {
  87.                     if (argarray[6] && (anchor->ap_Info.fib_DirEntryType > 0))
  88.                         anchor->ap_Flags |= APF_DODIR;
  89.                     mask = anchor->ap_Info.fib_Protection;
  90.                     mask ^= FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  91.                     if (argarray[4])
  92.                         mask |= flags;
  93.                     else if (argarray[5])
  94.                         mask &= (~flags);
  95.                     else
  96.                         mask = flags;
  97.                     mask ^= FIBF_READ|FIBF_WRITE|FIBF_EXECUTE|FIBF_DELETE;
  98.                     dir = CurrentDir(DupLock(anchor->ap_Last->an_Lock));
  99.                     if ((mask & muFIBF_SET_UID) &&
  100.                          (mask & (FIBF_GRP_WRITE | FIBF_OTR_WRITE)))
  101.                         PutStr(GetLocS(&li,MSG_WARN_UW));
  102.                     if (!muSetProtection(anchor->ap_Info.fib_FileName, mask)) {
  103.                         PutStr(anchor->ap_Buf);
  104.                         PrintFault(IoErr(), " ");
  105.                     } else if (!argarray[7]) {
  106.                         PutStr(anchor->ap_Buf);
  107.                         if (anchor->ap_Info.fib_DirEntryType > 0)
  108.                             PutStr("(Dir)");
  109.                         PutStr(GetLocS(&li,MSG_DONE));
  110.                     }
  111.                     UnLock(CurrentDir(dir));
  112.                 }
  113.             while (!(error = MatchNext(anchor)));
  114.             if (error == ERROR_NO_MORE_ENTRIES)
  115.                 error = NULL;
  116.         } else if (error == ERROR_NO_MORE_ENTRIES)
  117.             error = ERROR_OBJECT_NOT_FOUND;
  118.         MatchEnd(anchor);
  119.         FreeVec(anchor);
  120.     } else
  121.         error = IoErr();
  122.  
  123.  
  124.     FreeArgs(args);
  125.     if (error) {
  126.         PrintFault(error, NULL);
  127.         rc = RETURN_ERROR;
  128.     }
  129.  
  130.     CloseLoc(&li);
  131.  
  132. Exit:
  133.     CloseLibrary((struct Library *)muBase);
  134.     CloseLibrary((struct Library *)DOSBase);
  135.  
  136.     return(rc);
  137. }    
  138.  
  139.  
  140. static BOOL __regargs ProcessFlags(char *str, ULONG *flags, ULONG type,
  141.                                               struct DosLibrary *DOSBase,
  142.                                               struct LocaleInfo *li,
  143.                                               struct ExecBase *SysBase)
  144. {
  145.     int i;
  146.     BOOL rc = TRUE;
  147.  
  148.     for (i = 0; str[i] && rc; i++) {
  149.         switch(str[i]) {
  150.             case 'u':
  151.             case 'U':
  152.                 if (type == FLAGS_OWNER)
  153.                     *flags |= muFIBF_SET_UID;
  154.                 else
  155.                     goto Error;
  156.                 break;
  157.  
  158.             case 's':
  159.             case 'S':
  160.                 if (type == FLAGS_OWNER)
  161.                     *flags |= FIBF_SCRIPT;
  162.                 else
  163.                     goto Error;
  164.                 break;
  165.  
  166.             case 'p':
  167.             case 'P':
  168.                 if (type == FLAGS_OWNER)
  169.                     *flags |= FIBF_PURE;
  170.                 else
  171.                     goto Error;
  172.                 break;
  173.  
  174.             case 'a':
  175.             case 'A':
  176.                 if (type == FLAGS_OWNER)
  177.                     *flags |= FIBF_ARCHIVE;
  178.                 else
  179.                     goto Error;
  180.                 break;
  181.  
  182.             case 'r':
  183.             case 'R':
  184.                 if (type == FLAGS_OWNER)
  185.                     *flags |= FIBF_READ;
  186.                 else if (type == FLAGS_GROUP)
  187.                     *flags |= FIBF_GRP_READ;
  188.                 else
  189.                     *flags |= FIBF_OTR_READ;
  190.                 break;
  191.  
  192.             case 'w':
  193.             case 'W':
  194.                 if (type == FLAGS_OWNER)
  195.                     *flags |= FIBF_WRITE;
  196.                 else if (type == FLAGS_GROUP)
  197.                     *flags |= FIBF_GRP_WRITE;
  198.                 else
  199.                     *flags |= FIBF_OTR_WRITE;
  200.                 break;
  201.  
  202.             case 'e':
  203.             case 'E':
  204.                 if (type == FLAGS_OWNER)
  205.                     *flags |= FIBF_EXECUTE;
  206.                 else if (type == FLAGS_GROUP)
  207.                     *flags |= FIBF_GRP_EXECUTE;
  208.                 else
  209.                     *flags |= FIBF_OTR_EXECUTE;
  210.                 break;
  211.  
  212.             case 'd':
  213.             case 'D':
  214.                 if (type == FLAGS_OWNER)
  215.                     *flags |= FIBF_DELETE;
  216.                 else if (type == FLAGS_GROUP)
  217.                     *flags |= FIBF_GRP_DELETE;
  218.                 else
  219.                     *flags |= FIBF_OTR_DELETE;
  220.                 break;
  221.  
  222.             default:
  223. Error:
  224.                 rc = FALSE;
  225.                 PutStr(GetLocS(li,MSG_INVALID_FLAG));
  226.                 PutStr(" ");
  227.                 if (type == FLAGS_OWNER)
  228.                     PutStr("USPARWED\n");
  229.                 else
  230.                     PutStr("RWED\n");
  231.                 break;
  232.         }
  233.     }
  234.  
  235.     return(rc);
  236. }
  237.